<template>
  <div class="changepassword">
    <el-card>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原登录密码" prop="oldpassword">
          <el-input
            type="password"
            v-model="ruleForm.oldpassword"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="设置新密码" prop="newpassword">
          <el-input
            type="password"
            v-model="ruleForm.newpassword"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="newpassword2">
          <el-input
            type="password"
            v-model="ruleForm.newpassword2"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码密码"));
      } else {
        if (this.ruleForm.newpassword !== "") {
          this.$refs.ruleForm.validateField("newpassword2");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次确定新密码"));
      } else if (value !== this.ruleForm.newpassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        newpassword: "",
        newpassword2: "",
        oldpassword: sessionStorage.getItem("loginpassword"),
        id: sessionStorage.getItem("id")
      },
      rules: {
        newpassword: [{ validator: validatePass, trigger: "blur" }],
        newpassword2: [{ validator: validatePass2, trigger: "blur" }],
        oldpassword: [
          { required: true, message: "请输入原登录密码", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$http
            .post("/userchanepassword", this.ruleForm)
            .then(res => {
              // console.log(res);
              if (res.status == 200) {
                this.$message({
                  message: "登录密码修改成功...",
                  type: "success"
                });
                this.$router.push({ path: "/login" });
              }
              else{
                  this.$message({
                  message: "原始密码不正确...",
                  type: "error"
                });
              }
            })
            .catch(function() {});
        } else {
          this.$message({
            message: "修改密码失败...",
            type: "error"
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
</style>